package com.zh.web.controller;

import com.zh.business.service.web.user.IDPermissionService;
import com.zh.zhbootcommon.entity.user.User;
import com.zh.zhbootcommon.utils.common.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 描述: 主页
 *
 * @author: yanglin
 * @Date: 2021-08-04-14:11
 * @Version: 1.0
 */
@Slf4j
@Controller
@RequestMapping("")
public class IndexController {

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    IDPermissionService permissionService;

    /**
     * 登录
     *
     * @return
     */
    @RequestMapping("/login")
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView("login");
        return modelAndView;
    }

    /**
     * 登出
     *
     * @return
     */
    @RequestMapping("/logout")
    public ModelAndView logout(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("login");
        User user = (User) request.getSession().getAttribute("user");
        String token = (String) request.getSession().getAttribute("token");
        if (Objects.nonNull(user)){
            String userkey = "user:"+token;
            redisUtils.del(userkey);
        }
        request.getSession().removeAttribute("user");
        request.getSession().removeAttribute("menuList");
        return modelAndView;
    }

    /**
     * 主页
     *
     * @return
     */
    @RequestMapping("/home")
    public ModelAndView home(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("home");
        User user = (User) request.getSession().getAttribute("user");
        if (Objects.nonNull(user)) {
            //List<DPermission> permissions = permissionService.queryMenuByUser(user.getUserId());
            // 用户信息
            modelAndView.addObject("loginUser", user);
            // 用户菜单
            //modelAndView.addObject("menuList", permissions);
        }else{
            modelAndView = new ModelAndView("redirect:/logout");
        }
        return modelAndView;
    }

    /**
     * 等级
     *
     * @return
     */
    @RequestMapping("/level")
    public ModelAndView level() {
        ModelAndView modelAndView = new ModelAndView("level/level");
        return modelAndView;
    }

    /**
     * 用户
     *
     * @return
     */
    @RequestMapping("/user")
    public ModelAndView user(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("user/user");
        User user = (User) request.getSession().getAttribute("user");
        if (Objects.nonNull(user)) {
            // 用户信息
            if (user.getRoles().size() > 0){
                List<String> roleIds = user.getRoles().stream().map(r -> String.valueOf(r.getRoleId()))
                        .collect(Collectors.toList());
                String roleIdStr = roleIds.stream().collect(Collectors.joining(","));
                modelAndView.addObject("roleIdStr", roleIdStr);
            }else{
                modelAndView.addObject("roleIdStr", "");
            }
        }
        return modelAndView;
    }

    /**
     * 角色
     *
     * @return
     */
    @RequestMapping("/role")
    public ModelAndView role() {
        ModelAndView modelAndView = new ModelAndView("role/role");
        return modelAndView;
    }

    /**
     * 权限菜单
     *
     * @return
     */
    @RequestMapping("/permission")
    public ModelAndView permission() {
        ModelAndView modelAndView = new ModelAndView("permission/permission");
        return modelAndView;
    }

    /**
     * 司机
     *
     * @return
     */
    @RequestMapping("/motorman")
    public ModelAndView motorman() {
        ModelAndView modelAndView = new ModelAndView("motorman/motorman");
        return modelAndView;
    }

    /**
     * 乘客
     *
     * @return
     */
    @RequestMapping("/passenger")
    public ModelAndView passenger() {
        ModelAndView modelAndView = new ModelAndView("passenger/passenger");
        return modelAndView;
    }

    /**
     * 车辆
     *
     * @return
     */
    @RequestMapping("/car")
    public ModelAndView car() {
        ModelAndView modelAndView = new ModelAndView("car/car");
        return modelAndView;
    }

    /**
     * 订单
     *
     * @return
     */
    @RequestMapping("/order")
    public ModelAndView order() {
        ModelAndView modelAndView = new ModelAndView("order/order");
        return modelAndView;
    }

    /**
     * 地址
     *
     * @return
     */
    @RequestMapping("/address")
    public ModelAndView address() {
        ModelAndView modelAndView = new ModelAndView("address/address");
        return modelAndView;
    }


}
